var grid;
function initGrid(staffRoleId){
	grid = $("#maingrid").ligerGrid({
		url:HTR.U('index/goods/pageQuery'),
		pageSize:HTR.pageSize,
		pageSizeOptions:HTR.pageSizeOptions,
		height:'99%',
        width:'100%',
        minColToggle:6,
        rownumbers:true,
        columns: [
	        { display: '产品编号', name: 'goodsSn',isSort: false,width:150},
	        { display: '产品名称', name: 'goodsName',align:'left',isSort: false},
	        { display: '成本价格', name: 'costPrice',isSort: false,width:80},
	        { display: '销售价格', name: 'salePrice',isSort: false,width:80},
	        { display: '利润', name: 'profit',isSort: false,width:80},
	        { display: '单位', name: 'goodsUnit',isSort: false,width:80},
	        { display: '总库存', name: 'goodsStock',isSort: false,width:80},
	        { display: '总销量', name: 'saleNum',isSort: false,width:80},
	        { display: '操作', name: 'op',isSort: false,width:100,render: function (rowdata, rowindex, value){
	            var h = "";
	            if(HTR.GRANT.SPGL_01 && staffRoleId==1)h += "<a href='javascript:toEdit(" + rowdata['goodsId'] + ")'>编辑</a> ";
	            if(HTR.GRANT.SPGL_03 && staffRoleId==1)h += "<a href='javascript:del(" + rowdata['goodsId'] + ")'>删除</a> "; 
	            return h;
	        }}
        ]
    });
}
function loadGrid(){
	var params = HTR.getParams('.j-ipt');
	grid.set('url',HTR.U('index/goods/pageQuery',params));
}

function del(id){
	var box = HTR.confirm({content:"您确定要删除该产品吗?",yes:function(){
   var loading = HTR.msg('正在提交数据，请稍后...', {icon: 16,time:60000});
   $.post(HTR.U('index/goods/del'),{id:id},function(data,textStatus){
   			layer.close(loading);
   			var json = HTR.toIndexJson(data);
   			if(json.status=='1'){
   			    HTR.msg(json.msg,{icon:1});
   			    layer.close(box);
   			    grid.reload();
   			}else{
   			    HTR.msg(json.msg,{icon:2});
   			}
   		});
    }});
}
function toEdit(id){
	location.href=HTR.U('index/goods/toEdit','id='+id);
}
function save(){
	$('#editFrom').isValid(function(v){
		if(v){
			var params = HTR.getParams('.ipt');
			var loading = HTR.msg('正在提交数据，请稍后...', {icon: 16,time:60000});
		    $.post(HTR.U('index/goods/'+((params.goodsId==0)?"add":"edit")),params,function(data,textStatus){
		    	layer.close(loading);
		    	var json = HTR.toIndexJson(data);
		    	if(json.status=='1'){
		    		HTR.msg("操作成功",{icon:1});
		    		location.href=HTR.U('index/goods/index');
		    	}else{
		    		HTR.msg(json.msg,{icon:2});
		    	}
		    });
		}
	});
}